package com.server.common.code.utils;

import cn.hutool.core.io.FileUtil;
import com.server.common.code.error.BusinessException;

import java.util.Arrays;
import java.util.List;

public class UrlUtils {

    private static final List<String> IMAGE_SUFFIX_LIST = Arrays.asList(
            ".jpg",
            ".png",
            ".jpeg"
    );

    /**
     * 判断是否是图片  0表示成功   1表示后缀错误  2表示不是图片
     * @Author LXC
     * @Date 2022/2/10 8:42
     **/
    public static void checkImageUrl(List<String> fileList) {
        checkImageUrl(fileList,IMAGE_SUFFIX_LIST);
    }

    /**
     * 判断是否是图片  0表示成功   1表示后缀错误  2表示不是图片
     * @Author LXC
     * @Date 2022/2/10 8:42
     **/
    public static void checkImageUrl(List<String> fileList,List<String> imageSuffixList) {
        for (String file : fileList) {
            String originalFilename = OptionalUtils.ofEmpty(file).orElse("");
            String originalFilenameSuffix = FileUtil.getSuffix(originalFilename);

            //判断后缀
            long count = imageSuffixList.stream()
                    .filter(image -> image.contains(originalFilenameSuffix))
                    .count();

            if (count == 0) {
                throw new BusinessException("包含图片格式错误!");
            }

        }
    }



}
